package com.zy.springboot.service.book;

import com.alibaba.fastjson.JSONObject;
import com.zy.springboot.com.FastJsonConvert;
import com.zy.springboot.com.HttpUtil;
import com.zy.springboot.dao.book.BookDao;
import com.zy.springboot.dao.book.RecordsDao;
import com.zy.springboot.dao.user.UserDao;
import com.zy.springboot.entity.book.RecordsEntity;
import com.zy.springboot.entity.user.UserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static com.zy.springboot.com.Const.PageConst;

/**
 * Created by y on 2019/1/11.
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class RecordsServiceImpl implements RecordsService {
    @Autowired
    private RecordsDao recordsDao;

    @Override
    public String add(String jsonData) {
        JSONObject jsonObject= FastJsonConvert.convertJSONToObject(jsonData,JSONObject.class);
        String bookId=jsonObject.getString("bookId");
        String openId=jsonObject.getString("openId");
        String index="1";
        return null;
    }

    @Override
    public String update(String jsonData) {
        JSONObject jsonObject= FastJsonConvert.convertJSONToObject(jsonData,JSONObject.class);
        String bookId=jsonObject.getString("bookId");
        String openId=jsonObject.getString("openId");
        String index=jsonObject.getString("index");
        String total=jsonObject.getString("total");
        String id=recordsDao.check(bookId,openId).get(0).getString("id");
        RecordsEntity recordsEntity=(RecordsEntity)recordsDao.getIBaseEntityById(id);
        recordsEntity.setIndex2(index);
        return recordsDao.update(recordsEntity).toString();
    }

    @Override
    public JSONObject getList(String jsonData) {
        return null;
    }

    @Override
    public JSONObject check(String jsonData) {
        JSONObject jsonObject= FastJsonConvert.convertJSONToObject(jsonData,JSONObject.class);
        String bookId=jsonObject.getString("bookId");
        String openId=jsonObject.getString("openId");
        JSONObject object=new JSONObject();
        if(recordsDao.check(bookId,openId).size()>0){
            object.put("index2",recordsDao.check(bookId,openId).get(0).getString("index2"));
        }else{
            RecordsEntity recordsEntity=new RecordsEntity();
            recordsEntity.setBookId(bookId);
            recordsEntity.setOpenId(openId);
            recordsEntity.setIndex2("1");
            recordsEntity.setDelFlag(0);
            recordsDao.add(recordsEntity);
            object.put("index2","1");
        }
        return object;
    }
}
